package com.example.a313ldy.yuejian_17.Model;


import com.example.a313ldy.yuejian_17.Bean.Contest;
import com.example.a313ldy.yuejian_17.Gloablecalss.Yuejian;
import com.example.a313ldy.yuejian_17.Interface.ContestInterface;
import com.example.a313ldy.yuejian_17.Listener.ContestListener;
import com.example.a313ldy.yuejian_17.Service.ContestService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LDY on 2017/10/18.
 */

public class ContestModel implements ContestInterface {
    private ContestService service;
    private Retrofit retrofit;

    public ContestModel() {
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Yuejian.URL)
                .build();

    }




    @Override
    public void ContestData(final ContestListener<Contest> listener) {
        service= retrofit.create(ContestService.class);
        Call<Contest> call = service.getContest();
        //Retrofit使用-3
        call.enqueue(new Callback<Contest>() {
            @Override
            public void onResponse(Call<Contest> call, Response<Contest> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<Contest> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }


}
